home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / CONSTRAI / OP3_IMPL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  14.0 KB  |  381 lines

  1.  
  2. package sub_arctic.constraints;
  3.  
  4. import sub_arctic.lib.interactor;
  5. import sub_arctic.lib.interactor_consts;
  6. import sub_arctic.lib.manager;
  7. import sub_arctic.lib.sub_arctic_error;
  8.  
  9. import java.util.Vector;
  10.  
  11. /** 
  12.  * Constraint implementation class to provide encoding for standard 3 operand 
  13.  * constraints.  This object takes 3 std_objpart_encoding objects representing 
  14.  * operands, along with an unsigned 8 bit value to provide a constant to the 
  15.  * function.<p>
  16.  *
  17.  * 3 operand constraints are encoded as:
  18.  * <pre>
  19.  *      8        6      6      6       5
  20.  *   KKKKKKKK AAAAAA BBBBBB CCCCCC X 00000  
  21.  * </pre>
  22.  * where KKK represents an 8 bit unsigned constant, AAA, BBB, and CCC 
  23.  * represent parameter objects, and X represents one bit used to encode an 
  24.  * op code (i.e, clip or wrap in the standard set).<p>
  25.  *
  26.  * @author Scott Hudson
  27.  */
  28.  
  29. public class op3_impl implements std_encoding_consts {
  30.  
  31.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  32.  
  33.   /** 
  34.    * Create a standard constraint given an op_code, 3 obj/part designators, 
  35.    * and a constant.<p>
  36.    * 
  37.    * @param int                  op_code op code value for this operation.
  38.    * @param std_objpart_encoding op1     designator for operand 1
  39.    * @param std_objpart_encoding op2     designator for operand 2
  40.    * @param std_objpart_encoding op3     designator for operand 3
  41.    * @param short                K       value for 15 bit signed constant
  42.    */
  43.   public static std_constraint create(
  44.     int                  op_code, 
  45.     std_objpart_encoding op1, 
  46.     std_objpart_encoding op2, 
  47.     std_objpart_encoding op3, 
  48.     short                K) 
  49. {
  50.       return new std_constraint(encode(op_code,op1,op2,op3,K), 
  51.     op1.orientation() | op2.orientation() | op3.orientation());
  52.     }
  53.  
  54.    //had:
  55.    //* @exception bad_constraint if the op code is out of range.
  56.  
  57.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  58.  
  59.   /** 
  60.    * Do an encoding given an op_code, 3 obj/part designators, and a constant.
  61.    * 
  62.    * @param int                  op_code op code value for this operation.
  63.    * @param std_objpart_encoding op1     designator for operand 1
  64.    * @param std_objpart_encoding op2     designator for operand 2
  65.    * @param std_objpart_encoding op3     designator for operand 3
  66.    * @param short                K       value for 15 bit signed constant
  67.    */
  68.   public static int encode(int op_code, 
  69.     std_objpart_encoding op1, 
  70.     std_objpart_encoding op2, 
  71.     std_objpart_encoding op3, 
  72.     short K) 
  73. {
  74.       if (op_code < OP3_MIN || op_code > OP3_MAX)
  75.     throw new sub_arctic_error(
  76.      "Unrecognized op code (" + op_code + ") used for 3 op constraint");
  77.  
  78.       return (((int)K)<<24) | ((op1.encoding() & OBJPART_MASK) << 18) | 
  79.                               ((op2.encoding() & OBJPART_MASK) << 12) | 
  80.                               ((op3.encoding() & OBJPART_MASK) << 6) | 
  81.                           ((op_code<<OP3_SHIFT) & OP3_MASK)  | OP3_LOBITS;
  82.     }
  83.  
  84.    //had:
  85.    //* @exception bad_constraint if the op code is out of range.
  86.  
  87.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  88.  
  89.   /** 
  90.    * Decode op_code from the given encoding.  We assume that this is already
  91.    * known to be a 3-op constraint -- otherwise incorrect results will be 
  92.    * returned.<p>
  93.    *
  94.    * @param int enc the encoding for a constraint
  95.    */
  96.   public static int op_code(int enc) { return (enc & OP3_MASK) >> OP3_SHIFT; }
  97.  
  98.   /** 
  99.    * Decode first operand from the given encoding.  We assume that this is 
  100.    * already known to be a 3-op constraint -- otherwise incorrect results 
  101.    * will be returned.<p>
  102.    *
  103.    * @param int enc the encoding for a constraint
  104.    */
  105.   public static int op1(int enc) { return (enc >> 18) & OBJPART_MASK; }
  106.  
  107.   /** 
  108.    * Decode second operand from the given encoding.  We assume that this is 
  109.    * already known to be a 3-op constraint -- otherwise incorrect results 
  110.    * will be returned.<p>
  111.    *
  112.    * @param int enc the encoding for a constraint
  113.    */
  114.   public static int op2(int enc) { return (enc >> 12) & OBJPART_MASK; }
  115.  
  116.   /** 
  117.    * Decode third operand from the given encoding.  We assume that this is 
  118.    * already known to be a 3-op constraint -- otherwise incorrect results 
  119.    * will be returned.<p>
  120.    *
  121.    * @param int enc the encoding for a constraint
  122.    */
  123.   public static int op3(int enc) { return (enc >> 6) & OBJPART_MASK; }
  124.  
  125.   /** 
  126.    * Decode constant from the given encoding.  We assume that this is 
  127.    * already known to be a 3-op constraint -- otherwise incorrect results 
  128.    * will be returned.<p>
  129.    *
  130.    * @param int enc the encoding for a constraint
  131.    */
  132.   public static int const_val(int enc) { return (enc >> 24) & 0xff; }
  133.  
  134.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  135.  
  136.   /* Part constants copied over from interactor_consts for convenience. */
  137.   public static final int X = interactor_consts.X;
  138.   public static final int Y = interactor_consts.Y;
  139.   public static final int W = interactor_consts.W;
  140.   public static final int H = interactor_consts.H;
  141.  
  142.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  143.  
  144.   /**
  145.    * Evaluate an encoded constraint function given its operand and constant
  146.    * values.  We assume here that it is has already been determined that
  147.    * the constraint is a 3-op constraint.<p>
  148.    *
  149.    * @param int        enc         the encoding from the constraint
  150.    * @param interactor constr_obj  the object being constrained
  151.    * @param int        val1        value of first operand
  152.    * @param int        val2        value of second operand
  153.    * @param int        val3        value of third operand
  154.    * @param int        cnst_val    value of constant parameter
  155.    * @param int        orient      orientation of the constraint (should be 
  156.    *                               HORIZONTAL or VERTICAL).
  157.    * @return int the result of the evaluation
  158.    */
  159.   public static int eval_fun(
  160.     int enc,
  161.     interactor constr_obj,
  162.     int        val1,
  163.     int        val2,
  164.     int        val3,
  165.     int        cnst_val, 
  166.     int        orient) 
  167. {
  168.       /* sanity check */
  169.       if (constr_obj == null) 
  170.     throw new sub_arctic_error("Null constrained object passed to " + 
  171.                 "op3_impl.eval_fun()");
  172.  
  173.       /* execute code for the encoded function */
  174.       switch(op_code(enc))
  175.     {
  176.           case OP_clip: return 0; //xx later 
  177.           case OP_wrap: return 0; //xx later 
  178.  
  179.       default:
  180.         /* something is wrong if we get here */
  181.         throw new sub_arctic_error("Improperly encoded constraint found in "+
  182.                      "op3_impl.eval_fun()");
  183.     }
  184.     }
  185.  
  186.    //had:
  187.    //* @exception bad_value if the encoding, or constrained object/part are 
  188.    //*                      malformed
  189.  
  190.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  191.  
  192.    /** 
  193.     * Evaluate an encoded constraint applied to the given part of the given
  194.     * object.  We assume here that it it has already been determined that
  195.     * the constraint is a 3-op constraint.<p>
  196.     *
  197.     * @param int        enc         the encoding from the constraint
  198.     * @param interactor constr_obj  the object being constrained
  199.     * @param int        constr_part the part being constrained
  200.     * @param int        orient      the orientation of the constraint
  201.     * @return int the result of the evaluation
  202.     */
  203.    public static int eval(
  204.      int        enc, 
  205.      interactor constr_obj, 
  206.      int        constr_part,
  207.      int        orient) 
  208. {
  209.        int               cnst_val, val1, val2, val3; 
  210.  
  211.        /* sanity check */
  212.        if (constr_obj == null) 
  213.      throw new sub_arctic_error("Null constrained object passed to " + 
  214.                  "op1_impl.eval()");
  215.  
  216.        /* extract the constant */
  217.        cnst_val = const_val(enc);
  218.  
  219.        /* get the value for the operands */
  220.        val1 = std_constraint_impl.the_impl().fetch_value(
  221.                          op1(enc), constr_obj, orient);
  222.        val2 = std_constraint_impl.the_impl().fetch_value(
  223.                          op2(enc), constr_obj, orient);
  224.        val3 = std_constraint_impl.the_impl().fetch_value(
  225.                          op3(enc), constr_obj, orient);
  226.  
  227.        /* compute the value */
  228.        return eval_fun(enc, constr_obj, val1, val2, val3, cnst_val, orient);
  229.      }
  230.  
  231.     //had:
  232.     //* @exception bad_value if the encoding, or constrained object/part are 
  233.     //*                      malformed
  234.  
  235.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  236.  
  237.    /** Test whether the given encoded constraint (constraining the given 
  238.     *  object) depends on the indicated object and part (expressed relative
  239.     *  to it).  Here we assume that the encoding is already known to contain
  240.     *  a 3-op constraint.<p>
  241.     *
  242.     * @param int        enc             The encoding for the constraint.
  243.     * @param interactor constr_obj      The object the constraint is attached to
  244.     * @param int        test_which_obj  The object we are asking about.  This 
  245.     *                                   can be one of the values OBJCODE_SELF, 
  246.     *                                   OBJCODE_PARENT, OBJCODE_SOME_CHILD, 
  247.     *                                   OBJCODE_PREV_SIBLING, or 
  248.     *                                   OBJCODE_NEXT_SIBLING.  
  249.     * @param int        test_which_part The part we are asking about.
  250.     * @param int        nth_child       for SOME_CHILD, this provides the index
  251.     *                    of the child (and is ignored otherwise).
  252.     * @param int        orient          Orientation of the constraint.  This 
  253.     *                                   should be HORIZONTAL or VERTICAL.
  254.     * @return boolean whether the given constraint depends upon the given value
  255.     */
  256.    public static boolean depends_on(
  257.      int        enc, 
  258.      interactor constr_obj,
  259.      int        which_obj, 
  260.      int        which_part, 
  261.      int        nth_child,
  262.      int        orient) 
  263. {
  264.        int op;
  265.  
  266.        /* no special case implicit operands */
  267.  
  268.        /* have a look at the first operand dependency */
  269.        op = op1(enc);
  270.        if (std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  271.                             which_obj, which_part, nth_child, orient))
  272.      return true;
  273.  
  274.        /* if that didn't match try the second operand */
  275.        op = op2(enc);
  276.       if (std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  277.                    which_obj, which_part, nth_child, orient))
  278.     return true;
  279.  
  280.       /* finally, try the 3rd */
  281.       op = op3(enc);
  282.       return std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  283.                       which_obj, which_part, nth_child, orient);
  284.      }
  285.  
  286.     //had:
  287.     //* @exception bad_value if one of the parameters has an unrecognized value
  288.  
  289.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  290.  
  291.   /**
  292.    * Extract the set of objects/parts that a constraint depends on.  This
  293.    * produces a Vector with pairs of entries, the first being an interactor,
  294.    * and the second being an Integer which is the part number of that 
  295.    * interactor that is depended upon.<p>
  296.    *
  297.    * Here we assume that this is a 3 operand constraint.<p>
  298.    *
  299.    * @param int        enc        encoding value for the constraint in question.
  300.    * @param interactor constr_obj the object the constraint is attached to 
  301.    *                              (hence its referents are relative to).
  302.    * @param int        orient     Orientation of the constraint.  This 
  303.    *                              should be HORIZONTAL or VERTICAL.
  304.    * @return Vector containing pairs of objects, the first being an interactor
  305.    *                which is depended upon, and the second being an Integer
  306.    *                giving the part number of the part depended upon.
  307.    */
  308.   public static Vector mk_depend_list(int enc, interactor constr_obj,int orient) 
  309. {
  310.       int op;
  311.       Vector result;
  312.  
  313.       /* do implicit operands first */
  314.       result = mk_implicit_depend_list(enc, constr_obj, orient);
  315.  
  316.       /* extract the first operand and process that */
  317.       op = op1(enc);
  318.       std_constraint_impl.the_impl().
  319.                        add_depend_obj_part(result, op, constr_obj, orient);
  320.  
  321.       /* and the second */
  322.       op = op2(enc);
  323.       std_constraint_impl.the_impl().
  324.                        add_depend_obj_part(result, op, constr_obj, orient);
  325.  
  326.       /* and the third */
  327.       op = op3(enc);
  328.       std_constraint_impl.the_impl().
  329.                add_depend_obj_part(result, op, constr_obj, orient);
  330.  
  331.       return result;
  332.     }
  333.  
  334.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  335.  
  336.   /**
  337.    * Extract the set of objects/parts that a particular constraint function 
  338.    * implicitly depends on.  This produces a Vector with pairs of entries, the 
  339.    * first being an interactor, and the second being an Integer which is the 
  340.    * part number of that interactor that is depended upon.<p>
  341.    *
  342.    * @param int        enc        encoding value for the constraint in question.
  343.    * @param interactor constr_obj the object the constraint is attached to 
  344.    *                              (hence its referents are relative to).
  345.    * @param int        orient     Orientation of the constraint.  This 
  346.    *                              should be HORIZONTAL or VERTICAL.
  347.    * @return Vector containing pairs of objects, the first being an interactor
  348.    *                which is depended upon, and the second being an Integer
  349.    *                giving the part number of the part depended upon.
  350.    */
  351.   public static Vector mk_implicit_depend_list(
  352.     int enc, interactor constr_obj,int orient)
  353.     {
  354.       int    op;
  355.       Vector result = new Vector(9);
  356.  
  357.       /* no implicits at this point */
  358.  
  359.       return result;
  360.     }
  361.  
  362.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  363. }
  364.  
  365. /*=========================== COPYRIGHT NOTICE ===========================
  366.  
  367. This file is part of the subArctic user interface toolkit.
  368.  
  369. Copyright (c) 1996 Scott Hudson and Ian Smith
  370. All rights reserved.
  371.  
  372. The subArctic system is freely available for most uses under the terms
  373. and conditions described in 
  374.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  375. and appearing in full in the lib/interactor.java source file.
  376.  
  377. The current release and additional information about this software can be 
  378. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  379.  
  380. ========================================================================*/
  381.